package com.base.system.managedBean;

import java.util.List;

import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.operamasks.faces.annotation.Accessible;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.component.html.impl.UIIFrame;
import org.operamasks.faces.component.layout.impl.UIWindow;
import org.operamasks.faces.component.tree.base.TreeDataProvider;
import org.operamasks.faces.component.tree.impl.UITree;
import org.operamasks.faces.component.tree.impl.UITreeNode;

import com.base.commonKey.entity.CommonKey;
import com.base.commonKey.service.ICommonKeyService;
import com.base.commons.SpringBeanHolder;

@ManagedBean(name = "system_showCommonKeyTreeBean", scope = ManagedBeanScope.REQUEST)
public class ShowCommonKeyTreeBean {

	private final static Log logger = LogFactory
			.getLog(ShowCommonKeyTreeBean.class);
	private ICommonKeyService commonKeyService = SpringBeanHolder
			.getBean("commonKeyService");

	@SaveState
	@Accessible
	private String keyType = "";

	@Bind(id = "expertType", attribute = "value")
	@SaveState
	private String expertType;

	@Accessible
	private String toShowContent;

	@Inject
	FacesContext context;
	@Bind
	private UITree cks_selectTree;

	@Bind(id = "detailDialog")
	protected UIWindow detailDialog;

	@Bind(id = "copyDialog")
	protected UIWindow copyDialog;

	@Bind(id = "editDialog")
	protected UIWindow editDialog;

	@Bind(id = "addDialog")
	protected UIWindow addDialog;

	@Bind(id = "frame")
	private UIIFrame frame;

	@Bind(id = "copyFrame")
	private UIIFrame copyFrame;

	@Bind(id = "editFrame")
	private UIIFrame editFrame;

	@Bind(id = "addFrame")
	private UIIFrame addFrame;

	@BeforeRender
	public void beforeRender(boolean isPostback) {
		if (!isPostback) {
			keyType = context.getExternalContext().getRequestParameterMap()
					.get("keyType");
			logger.debug("keyType is:" + keyType);

			toShowContent = "";
			Object[] forShowObject = treeData_cks_selectTree
					.getChildren("通用选项");
			if (forShowObject != null && forShowObject.length > 0) {

			} else {
				toShowContent = "此类型还未添加配置项！";
			}
		}
	}

	@Bind(id = "cks_selectTree")
	private TreeDataProvider treeData_cks_selectTree = new TreeDataProvider() {

		public Object[] getChildren(Object node) {
			// logger.debug("keyType in treeProvider is:" + keyType);
			if (node == null) {
				return new String[] { "通用选项" };
			}
			if ("通用选项".equals(node.toString())) {
				List<CommonKey> temp = commonKeyService
						.getTopCommonDirectoryOfDeptForEdit(keyType);// getTopAll(keyType);
				return temp.toArray();
			}
			if (node instanceof CommonKey) {
				CommonKey ck = (CommonKey) node;

				List<CommonKey> dirList = commonKeyService
						.getDirectSubCommonDirectoryForEdit(String.valueOf(ck
								.getId()));

				if (!dirList.isEmpty())
					return dirList.toArray();
				return null;
			}
			return null;
		}

		public String getHref(Object nodeData) {
			if (nodeData instanceof CommonKey) {

				return null;
			}

			return null;
		}

		public String getHrefTarget(Object nodeData) {
			return "expertSortList";

		}

		public String getIcon(Object nodeData) {
			// if (nodeData instanceof CommonKey) {
			// CommonKey ck = (CommonKey) nodeData;
			// if (ck.getChildren() != null)
			// return "/images/folder.png";
			//
			// return "/images/listshow.gif";
			// }
			return "";
		}

		public String getText(Object nodeData) {

			if (nodeData instanceof CommonKey) {

				return ((CommonKey) nodeData).getKeyValue();
			}
			return null;
		}

		public boolean isCascade(Object nodeData) {
			return false;
		}

		public Boolean isChecked(Object nodeData) {
			return null;
		}

		public boolean isExpanded(Object nodeData) {
			return false;
		}

		public boolean isLeaf(Object nodeData) {
			return false;
		}
	};

	@Action
	public void cks_selectTree_onclick() {
		UITreeNode nodeData = cks_selectTree.getSelectedNode();
		Object object = nodeData.getUserData();
		String script = "";
		if (object instanceof CommonKey) {
			CommonKey ck = (CommonKey) object;
			if (ck != null) {

				detailDialog.setTitle("查看配置项");
				frame.load("/modules/system/showCommonKey.faces?commonKeyId="
						+ ck.getId());
				detailDialog.show();
			}
		}
	}

	@Action(id = "gridReload")
	public void gridReload() {
		cks_selectTree.setRootVisible(false);
		cks_selectTree.repaint();

		detailDialog.close();
		editDialog.close();
		copyDialog.close();
		addDialog.close();
	}

	@Action(id = "create")
	public void create() {
		detailDialog.close();
		UITreeNode nodeData = cks_selectTree.getSelectedNode();
		Object object = nodeData.getUserData();
		String script = "";
		if (object instanceof CommonKey) {
			CommonKey ck = (CommonKey) object;
			if (ck != null) {
				// System.out.println(ck.getId());
				copyDialog.setTitle("复制选项");
				copyFrame
						.load("/modules/system/showCopyCommonKey.faces?commonKeyId="
								+ ck.getId());
				copyDialog.show();
			}
		}
	}

	@Action(id = "edit")
	public void edit() {
		detailDialog.close();
		UITreeNode nodeData = cks_selectTree.getSelectedNode();
		Object object = nodeData.getUserData();
		String script = "";
		if (object instanceof CommonKey) {
			CommonKey ck = (CommonKey) object;
			if (ck != null) {
				// System.out.println(ck.getId());
				editDialog.setTitle("编辑配置选项");
				editFrame
						.load("/modules/system/editCommonKeyItem.faces?commonKeyId="
								+ ck.getId());
				editDialog.show();
			}
		}
	}

	@Action(id = "add")
	public void add() {
		addDialog.setTitle("添加配置项");
		// System.out.println("keyType" + keyType);
		addFrame.load("/modules/system/addCommonKeyForKnownType.faces?keyType="
				+ keyType);
		addDialog.show();
	}
}
